package com.bifang.module.file.core.biz.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bifang.engine.assistant.core.annotation.ApiService;
import com.bifang.engine.assistant.core.annotation.OpApi;
import com.bifang.engine.assistant.core.annotation.SaveOplog;
import com.bifang.engine.assistant.core.enums.AuTypeEnum;
import com.bifang.engine.assistant.core.enums.FuncTypeEnum;
import com.bifang.engine.data.core.util.PagerUtil;
import com.bifang.module.file.common.vo.fileFieldClass.FileFieldClassBatchVO;
import com.bifang.module.base.core.helper.DictHelper;
import com.bifang.module.file.common.enums.FileClassDictEnum;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import cn.hutool.core.convert.Convert;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.bifang.engine.assistant.core.exception.AppException;
import org.springframework.util.ObjectUtils;
import org.springframework.transaction.annotation.Transactional;
import com.bifang.engine.assistant.core.enums.BoolEnum;
import com.bifang.module.file.common.vo.fileFieldClass.FileFieldClassVO;
import com.bifang.module.file.common.vo.fileFieldClass.FileFieldClassKeyVO;
import com.bifang.module.file.common.vo.fileFieldClass.FileFieldClassQueryVO;
import com.bifang.module.file.core.biz.FileFieldClassIntegration;
import com.bifang.module.file.dao.service.FileFieldClassService;
import com.bifang.module.file.dao.model.FileFieldClass;
import org.springframework.util.StringUtils;

/**
 * 文件元数据字段分类表业务实现类
 *
 * @author xyh
 * @since 2023-03-16
 */
@ApiService(funcCode = "fileFieldClass", title = "文件元数据字段分类表")
public class FileFieldClassIntegrationImpl implements FileFieldClassIntegration {

    @Autowired
    public FileFieldClassService fileFieldClassService;
    @Autowired
    public DictHelper dictHelper;

    @Override
    @OpApi(funcCode = "fileFieldClass0001", title = "文件元数据字段分类表分页查询", funcType = FuncTypeEnum.query)
    public PageInfo<FileFieldClassVO> getList(FileFieldClassQueryVO vo) {
        // 开启分页
        if (vo.getPageNum() == null || vo.getPageSize() == null) {
            //如果不传入分页参数，默认最大取1000条
            vo.setPageNum(1);
            vo.setPageSize(1000);
        }
        PageHelper.startPage(vo.getPageNum(),vo.getPageSize());

        Map<String, Object> queryMap = BeanUtil.beanToMap(vo, false, true);
        List<FileFieldClassVO> list = fileFieldClassService.getQueryList(queryMap);
        if (CollectionUtils.isEmpty(list)) {
            // 判断是否为空，为空则返回空参数
            return PagerUtil.createEmptyPagerInfo(vo.getPageNum(), vo.getPageSize());
        }
        // 不为空则获取分页结果
        PageInfo<FileFieldClassVO> pageInfo = new PageInfo<>(list);
        // 返回参数需要通过common层的vo实体类，用该方法可以批量转换
//        List<FileFieldClassVO> resultList = Convert.toList(FileFieldClassVO.class, pageInfo.getList());
        // 转义数据字典值
        dictHelper.valueToName(list, Arrays.asList(FileClassDictEnum.values()));
        return PagerUtil.parsePagerVo(list, pageInfo);
    }

    @Override
    @OpApi(funcCode = "fileFieldClass0002", title = "文件元数据字段分类表根据主键查询详情", funcType = FuncTypeEnum.query)
    public FileFieldClassVO getById(FileFieldClassKeyVO vo) {
        FileFieldClass entity = fileFieldClassService.getById(vo.getFieldClassId());
        if (ObjectUtils.isEmpty(entity)) {
            throw new AppException("查询不到详细信息!");
        }
        FileFieldClassVO result = Convert.convert(FileFieldClassVO.class, entity);
        return result;
    }
    /** 保存前统一校验*/
    @Override
    @OpApi(funcCode = "fileFieldClass0003", title = "文件元数据字段分类表保存前校验", funcType = FuncTypeEnum.other ,checkPrivilege = BoolEnum.FALSE)
    public void checkSave(FileFieldClassVO vo) {
        //主键为空为新增校验

        //主键不为空为修改校验
    }

    @Override
    @OpApi(funcCode = "fileFieldClass0004", title = "文件元数据字段分类表新增", funcType = FuncTypeEnum.insert)
    @SaveOplog(
            operationType = "fileFieldClass0004",
            operationName = "文件元数据字段分类表新增",
            dataType = "fieldClassId",
            auType = AuTypeEnum.OTHER)
    @Transactional
    public FileFieldClassVO add(FileFieldClassVO vo) {
        //保存前校验
        checkSave(vo);
        //保存数据
        FileFieldClass entity = Convert.convert(FileFieldClass.class, vo);
        fileFieldClassService.save(entity);
        //将主键设置回vo使操作日志可以取到
        vo.setFieldClassId(entity.getFieldClassId());
        return vo;
    }

    @Override
    @OpApi(funcCode = "fileFieldClass0005", title = "文件元数据字段分类表修改", funcType = FuncTypeEnum.update)
    @SaveOplog(
            operationType = "fileFieldClass0005",
            operationName = "文件元数据字段分类表修改",
            dataType = "fieldClassId",
            auType = AuTypeEnum.OTHER)
    @Transactional
    public FileFieldClassVO edit(FileFieldClassVO vo) {
        if (ObjectUtils.isEmpty(vo.getFieldClassId())) {
            throw new AppException("传入需要修改的主键不能为空!");
        }
        FileFieldClass oldEntity = fileFieldClassService.getById(vo.getFieldClassId());
        if (ObjectUtils.isEmpty(oldEntity)) {
            throw new AppException("查询不到需要修改的信息!");
        }
        //保存前校验
        checkSave(vo);
        //保存数据
        FileFieldClass entity = Convert.convert(FileFieldClass.class, vo);
        fileFieldClassService.updateById(entity);
        return vo;
    }

    @Override
    @OpApi(funcCode = "fileFieldClass0006", title = "文件元数据字段分类表根据主键删除", funcType = FuncTypeEnum.delete)
    @SaveOplog(
            operationType = "fileFieldClass0006",
            operationName = "文件元数据字段分类表删除",
            dataType = "fieldClassId",
            auType = AuTypeEnum.OTHER)
    @Transactional
    public void del(FileFieldClassKeyVO vo) {
        if (ObjectUtils.isEmpty(vo.getFieldClassId())) {
            throw new AppException("传入需要删除的数据主键不能为空!");
        }
        FileFieldClass entity = fileFieldClassService.getById(vo.getFieldClassId());
        if (ObjectUtils.isEmpty(entity)) {
            throw new AppException("未找到需要删除的信息!");
        }
        fileFieldClassService.removeById(vo.getFieldClassId());
    }

    @Override
    @OpApi(funcCode = "fileFieldClass0007", title = "文件元数据字段分类批量新增", funcType = FuncTypeEnum.insert)
    @SaveOplog(
            operationType = "fileFieldClass0007",
            operationName = "文件元数据字段分类批量新增",
            dataType = "fieldClassId",
            auType = AuTypeEnum.OTHER)
    @Transactional
    public void addBatch(FileFieldClassBatchVO vo) {
        if(StringUtils.isEmpty(vo.getFileClassCode())){
            throw new AppException("文件分类编码不能为空");
        }
        List<String> fieldIds = vo.getFieldIds();
        if(!CollectionUtils.isEmpty(fieldIds)){
            List<FileFieldClass> saveList=new ArrayList<>();
            for (String fieldId:fieldIds){
                FileFieldClass fileFieldClass = new FileFieldClass();
                fileFieldClass.setFieldId(Long.valueOf(fieldId));
                fileFieldClass.setFileClassCode(vo.getFileClassCode());
                saveList.add(fileFieldClass);
            }
            fileFieldClassService.saveBatch(saveList);
        }
    }

    @Override
    @OpApi(funcCode = "fileFieldClass0008", title = "文件元数据字段分类批量删除", funcType = FuncTypeEnum.delete)
    @SaveOplog(
            operationType = "fileFieldClass0008",
            operationName = "文件元数据字段分类批量删除",
            dataType = "fieldClassId",
            auType = AuTypeEnum.OTHER)
    @Transactional
    public void delBatch(FileFieldClassBatchVO vo) {
        if(StringUtils.isEmpty(vo.getFileClassCode())){
            throw new AppException("文件分类编码不能为空");
        }
        List<String> fieldIds = vo.getFieldIds();
        if(!CollectionUtils.isEmpty(fieldIds)){
            QueryWrapper<FileFieldClass> queryWrapper=new QueryWrapper<>();
            queryWrapper.lambda().in(FileFieldClass::getFieldId,fieldIds)
                    .eq(FileFieldClass::getFileClassCode,vo.getFileClassCode());
            fileFieldClassService.remove(queryWrapper);
        }
    }

    @Override
    @OpApi(funcCode = "fileFieldClass0009", title = "文件元数据字段分类已关联查询", funcType = FuncTypeEnum.query)
    public PageInfo<FileFieldClassVO> getListByClass(FileFieldClassQueryVO vo) {
        // 开启分页
        if (vo.getPageNum() == null || vo.getPageSize() == null) {
            //如果不传入分页参数，默认最大取1000条
            vo.setPageNum(1);
            vo.setPageSize(1000);
        }
        PageHelper.startPage(vo.getPageNum(),vo.getPageSize());

        List<FileFieldClassVO> list = fileFieldClassService.getListByClass(vo);
        if (CollectionUtils.isEmpty(list)) {
            // 判断是否为空，为空则返回空参数
            return PagerUtil.createEmptyPagerInfo(vo.getPageNum(), vo.getPageSize());
        }

        // 不为空则获取分页结果
        PageInfo<FileFieldClassVO> pageInfo = new PageInfo<>(list);
        // 转义数据字典值
        // dictHelper.valueToName(resultList, Arrays.asList(DemoDictEnum.values()));
        return PagerUtil.parsePagerVo(list, pageInfo);
    }
}
